package com.zlq.cn.controller.sign.ai;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlq.cn.api.SignApiResult;
import com.zlq.cn.bootstrap.SignApiBootstrap;
import com.zlq.cn.config.Config;
import com.zlq.cn.entity.AizhPicture;
import com.zlq.cn.entity.FanyiUser;
import com.zlq.cn.entity.SuggestRecord;
import com.zlq.cn.service.ai.AizhPictureService;
import com.zlq.cn.service.un.FanyiUserService;
import com.zlq.cn.service.un.SuggestRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhoucl
 * @since 2022-03-05
 */
@Api(tags = "AI作画")
@Validated
@RestController
@RequestMapping("/aizh/user")
public class AizhUserController {

    @Resource
    private FanyiUserService fanyiUserService;

    @Resource
    private AizhPictureService aizhPictureService;

    @Resource
    private SuggestRecordService suggestRecordService;

//    @Value("${aizh.open}")
//    private String open;

    @ApiOperation("获取服务器时间")
    @GetMapping("/sys/time")
    public SignApiResult<Long> sysTime() {
        return SignApiResult.resultSuccess(System.currentTimeMillis());
    }

    @ApiOperation("用户登录/注册")
    @PostMapping("/login")
    public SignApiResult<FanyiUser> login(@RequestBody Object o) {
        FanyiUser fanyiUser = fanyiUserService.login(Config.PROJECT_AIZH);
//        fanyiUser.setOpen(open);
        return SignApiResult.resultSuccess(fanyiUser);
    }

    @ApiOperation("反馈")
    @PostMapping("/suggest/add")
    public SignApiResult<String> suggestRecord(@RequestBody SuggestRecord suggestRecord) {
        suggestRecord.setProject(Config.PROJECT_AIZH);
        suggestRecord.setIsreply(0);
        suggestRecord.setType(SignApiBootstrap.get().getImei());
        suggestRecord.setSubmitdate(LocalDateTime.now());
        suggestRecordService.save(suggestRecord);
        return SignApiResult.resultSuccess("反馈成功");
    }

    @ApiOperation("AI作画列表")
    @GetMapping("/list")
    public SignApiResult<Page<AizhPicture>> list(Page<AizhPicture> page, AizhPicture aizhPicture) {
        aizhPictureService.lambdaQuery()
                .eq(StringUtils.isNotEmpty(aizhPicture.getStyle()), AizhPicture::getStyle, aizhPicture.getStyle())
                .like(StringUtils.isNotEmpty(aizhPicture.getText()), AizhPicture::getText, aizhPicture.getText())
                .orderByDesc(AizhPicture::getCreateTime)
                .page(page);
        return SignApiResult.resultSuccess(page);
    }

    @ApiOperation("新增AI作画")
    @PostMapping("/add")
    public SignApiResult<Boolean> add(@RequestBody AizhPicture aizhPicture) {
        if (!aizhPictureService.updateById(aizhPicture)) {
            aizhPictureService.save(aizhPicture);
        }
        return SignApiResult.resultSuccess(Boolean.TRUE);
    }

    @ApiOperation("修改AI作画")
    @PostMapping("/update")
    public SignApiResult<Boolean> update(@RequestBody AizhPicture aizhPicture) {
        return SignApiResult.resultSuccess(aizhPictureService.updateById(aizhPicture));
    }
}

